﻿namespace ImageP
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    
    /// <summary>
    /// class to normaliza an image
    /// </summary>
    public class Normalize
    {
        /// <summary>
        /// height of image
        /// </summary>
        private int height = 0;

        /// <summary>
        /// width of image
        /// </summary>
        private int width = 0;

        /// <summary>
        /// minimum values of red, green & blue
        /// </summary>
        private double minR, minG, minB;

        /// <summary>
        /// maximum values of red, green & blue
        /// </summary>
        private double maxR, maxG, maxB;

        /// <summary>
        /// normalizes coming image
        /// </summary>
        /// <param name="Buffer">Buffer</param>
        /// <param name="h">h</param>
        /// <param name="w">w</param>
        /// <returns>RGBCOLORS</returns>
        public RGBCOLORS[,] Normalize_image(RGBCOLORS[,] buffer, int h, int w)
        {
            RGBCOLORS[,] finalBuffer = new RGBCOLORS[h, w];
            this.height = h;
            this.width = w;
            this.minR = 0;
            this.minG = 0;
            this.minB = 0;
            this.maxR = 255;
            this.maxG = 255;
            this.maxB = 255;
            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    if (buffer[i, j].R < this.minR)
                    {
                        this.minR =  buffer[i, j].R;
                    }

                    if (buffer[i, j].R > this.maxR)
                    {
                        this.maxR = buffer[i, j].R;
                    }

                    if (buffer[i, j].G < this.minG)
                    {
                        this.minG = buffer[i, j].G;
                    }

                    if (buffer[i, j].G > this.maxG)
                    {
                        this.maxG = buffer[i, j].G;
                    }

                    if (buffer[i, j].B < this.minB)
                    {
                        this.minB = buffer[i, j].B;
                    }

                    if (buffer[i, j].B > this.maxB)
                    {
                        this.maxB = buffer[i, j].B;
                    }
                }
            }

            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    finalBuffer[i, j].R = ((((double)buffer[i, j].R - (double)this.minR) / ((double)this.maxR - (double)this.minR))) * 255;
                    finalBuffer[i, j].G = ((((double)buffer[i, j].G - (double)this.minG) / ((double)this.maxG - (double)this.minG))) * 255;
                    finalBuffer[i, j].B = ((((double)buffer[i, j].B - (double)this.minB) / ((double)this.maxB - (double)this.minB))) * 255;
                }
            }

            return finalBuffer;
        }
        public int[,] Normalize_imagefft(double[,] buffer, int h, int w)
        {
            int[,] finalBuffer = new int[h, w];
            this.height = h;
            this.width = w;
            this.minR = 255;
            this.maxR = 0;
            
            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    if (buffer[i, j] < this.minR)
                    {
                        this.minR = Convert.ToDouble( buffer[i, j]);
                    }

                    if (buffer[i, j]> this.maxR)
                    {
                        this.maxR = Convert.ToDouble( buffer[i, j]);
                    }

                }
            }
            double x;
            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    x = ((((double)buffer[i, j] - (double)this.minR) / ((double)this.maxR - (double)this.minR))) * 255;
                    finalBuffer[i, j] = Convert.ToInt32( x);
                }
            }

            return finalBuffer;
        }
       
    }
}
